package org.theqcp.wow.api.wowarmory.parsers;

import java.util.ArrayList;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.xpath.XPath;
import org.theqcp.wow.model.api.ProgressListener;
import org.theqcp.wow.model.items.Equipment;
import org.theqcp.wow.model.mechanics.Attribute;

/**
 * Parse equipment data from wowarmory xml
 * 
 * @author angryundead
 *
 */
public class EquipmentParser extends AbstractParser{

	public List<Equipment> parse(Document inputDocument) {
		return this.parse(inputDocument,null);
	}
	
	public List<Equipment> parse(Document inputDocument, ProgressListener listener) {
		
		List<Equipment> eqList = new ArrayList<Equipment>();

		try {

			//get element(s) with xpath
			XPath xpath = XPath.newInstance("/page/itemTooltips/itemTooltip");

			//list
			@SuppressWarnings("unchecked")
			List<Element> elementList = xpath.selectNodes(inputDocument);
			
			if(listener != null) {
				listener.updateProgress(0);
			}

			if(listener != null) {
				listener.updateProgress(10);
			}
			
			//90% in loop
			double remains = 90 / elementList.size();
			double percent = 10;
			
			//loop through list of elements (equipment)
			for(Element e : elementList) {
				
				//name
				String name = e.getChildTextTrim("name");
				String id = e.getChildTextTrim("id");
				
				//create if name and id are available
				if(id != null && !id.isEmpty() && name != null && !name.isEmpty()) {
					//create item
					Equipment item = new Equipment();
					
					//set item stats
					item.setItemName(name);
					item.setItemId(id);
					
					//damage
					if(e.getChild("damageData") != null && e.getChild("damageData").getChild("damage") != null) {
						String damageHigh = e.getChild("damageData").getChild("damage").getChildTextTrim("max");
						String damageLow = e.getChild("damageData").getChild("damage").getChildTextTrim("min");
						String speed = e.getChild("damageData").getChildTextTrim("speed");
						String dps = e.getChild("damageData").getChildTextTrim("dps");
						
						item.setStat(Attribute.DAMAGEHIGH, this.convertSafeDouble(damageHigh));
						item.setStat(Attribute.DAMAGELOW, this.convertSafeDouble(damageLow));
						item.setStat(Attribute.DPS, this.convertSafeDouble(dps));
						item.setStat(Attribute.SPEED, this.convertSafeDouble(speed));
					}
					
					//stats		
					String strength = e.getChildTextTrim("bonusStrength");
					String agility = e.getChildTextTrim("bonusAgility");
					String intellect = e.getChildTextTrim("bonusIntellect");
					String stamina = e.getChildTextTrim("bonusStamina");
					String spirit = e.getChildTextTrim("bonusSpirit");
					
					//set item attributes
					item.setStat(Attribute.STRENGTH, this.convertSafeDouble(strength));
					item.setStat(Attribute.AGILITY, this.convertSafeDouble(agility));
					item.setStat(Attribute.INTELLECT, this.convertSafeDouble(intellect));
					item.setStat(Attribute.STAMINIA, this.convertSafeDouble(stamina));
					item.setStat(Attribute.SPIRIT, this.convertSafeDouble(spirit));
			
					//save item to list
					if(item != null && item.getItemId() != null && !item.getItemId().isEmpty()) {
						eqList.add(item);
					}
					
					//update percentage
					percent += remains;
					if(listener != null) {
						listener.updateProgress(remains);
					}
				}
			}
			
			//done!
			if(listener != null) {
				listener.updateProgress(100);
			}
			
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		
		
		return eqList;
	}
	

}
